package algorithm;

public class Leetcode45 {

    public int jump(int[] nums) {
        if (nums == null || nums.length == 1) {
            return 0;
        }
        int curMaxPosition = nums[0];
        int curMax = nums[0];
        int step = 1;
        for (int i = 1; i < nums.length; i++) {
            if (curMax >= nums.length - 1) {
                break;
            }
            if (nums[i] + i > curMax) {
                curMax = nums[i] + i;
            }
            if (i == curMaxPosition && curMax > curMaxPosition) {
                step ++;
                curMaxPosition = curMax;
            }


        }

        return step;
    }

    public int jump2(int[] nums) {
        int len = nums.length;
        int end = 0;
        int maxPosition = 0;
        int steps = 0;
        for (int i = 0; i < len - 1; i++) {
            maxPosition = Math.max(maxPosition, i + nums[i]);
            if (i == end) {
                steps++;
                end = maxPosition;
            }
        }
        return steps;
    }
}
